home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / MUSIC / @FALCON / MP2_07 / SRC / GEM / MP2FILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-24  |  6.1 KB  |  356 lines

  1. #include <tos.h>
  2. #include <aes.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #include "snddefs.h"
  7. #include "mp2audio.h"
  8.  
  9. /* #define DEBUG */
  10.  
  11. typedef struct
  12. {
  13.     long bitrate,sample_frequency,filelength,timelength;
  14. } MP2INFO;
  15.  
  16. /* Functions in this module */
  17. int open_file(void);
  18. int reopen_file(void);
  19. void close_file(int fd);
  20. void reset_file(int fd);
  21. void real_load(long l, char *b);
  22. void load(int q);
  23. void set_stop(long *buf);
  24. int has_stopped(int state);
  25.  
  26.  
  27. /* global variables */
  28. char path[512]="\0", filename[512]="\0";
  29. long filesize,filepos,bufend=0;
  30.  
  31. /* global variables from mp2audio.c */
  32. extern int file_open,fd,quit,replay;
  33. extern long buffer,block;
  34. extern char buffer_mem[(long)BLOCK_SIZE];
  35.  
  36. /* global variable from mp2event.c */
  37. extern long total_time,savedtimerc;
  38. extern int looping;
  39.  
  40. /* global variable from mp2info.c */
  41. extern MP2INFO mp2info;
  42.  
  43. /* Function from mp2exit.c */
  44. extern void exit_replay(void);
  45.  
  46. /* Function from mp2init.c */
  47. extern void init_replay(void);
  48.  
  49. /* Function from mp2event.c */
  50. extern long set_timerc(void);
  51. extern long reset_timerc(void);
  52. extern void update_time(void);
  53.  
  54. int open_file(void)
  55. {
  56.     int button,ret;
  57.     char tmp[1024];
  58.  
  59.     if(!path[0])
  60.     {
  61.         path[0] = 'A' + Dgetdrv();    /* initialize directory pathname */
  62.         path[1] = ':';
  63.         Dgetpath( path+2, 0 );
  64.         strcat( path, "\\*.MP2" );
  65.     }
  66.     
  67.     fsel_exinput(path, filename, &button, "Load MPEG");
  68.     if (button == 1) {
  69.         if(replay)
  70.             exit_replay();
  71.         if(file_open)
  72.             close_file(fd);
  73.         strcpy(tmp,path);
  74.         strrchr(tmp, '\\')[1] = '\0';
  75.         if((ret=(int) Fopen(strcat(tmp, filename), FO_READ)) > 0)
  76.         {
  77.             file_open=1;
  78.             filesize=Fseek(0L,ret,2);
  79.             Fseek(0L,ret,0);
  80.         }
  81.         return ret;
  82.     }
  83.     return 0;
  84. }
  85.  
  86. int reopen_file()
  87. {
  88.     char tmp[1024];
  89.     int tfd;
  90.     long fp;
  91.  
  92.     strcpy(tmp,path);
  93.     strrchr(tmp, '\\')[1] = '\0';
  94.     file_open=1;
  95.     tfd=(int)Fopen(strcat(tmp, filename), FO_READ);
  96.     fp=Fseek(filepos,tfd,0);
  97.  
  98.     fp=fp;
  99. #ifdef DEBUG
  100.     sprintf(tmp,"[1][New filepos: %ld|Should be: %ld][Ok]",fp,filepos);
  101.     form_alert(1,tmp);
  102. #endif
  103.     return tfd;
  104. }
  105.  
  106. void close_file(int fd)
  107. {
  108.     file_open=0;
  109.     Fclose(fd);
  110. }
  111.  
  112. void reset_file(int fd)
  113. {
  114.     Fseek(0L,fd,0);
  115. }
  116.  
  117.  
  118.  
  119. void real_load(long l, char *b)
  120. {
  121.     long a=l;
  122.     long tmp,t=(long)b;
  123.  
  124. #ifdef DEBUG
  125.     long ffp;
  126.     char tt[128];
  127. #endif
  128.  
  129.     while (a > 0)
  130.     {
  131.  
  132. #ifdef DEBUG
  133.         ffp=Fseek(0L,fd,1);
  134.         sprintf(tt,"[1][%d: a=%ld|t=%lx fpos=%ld][Ok]",fd,a,t,ffp);
  135.         puts(tt);
  136. #endif
  137.             
  138.         tmp=Fread(fd, a & 0xfffffffeL, (void *)t);
  139.  
  140. #ifdef DEBUG
  141.         ffp=Fseek(0L,fd,1);
  142.         sprintf(tt,"[1][%d Read tmp=%ld|a=%ld|t=%lx fpos=%ld][Ok]",fd,tmp,a,t,ffp);
  143.         puts(tt);
  144. #endif
  145.  
  146.         if (tmp < 0)
  147.         {
  148. #ifdef DEBUG
  149.             ffp=Fseek(0L,fd,1);
  150.             sprintf(tt,"[1][%d reset tmp<0|a=%ld t=%lx|tmp=%ld fpos=%ld][Ok]",fd,a,t,tmp,ffp);
  151.             puts(tt);
  152. #endif
  153.             bufend=t;
  154.             for( ; a>0 ; a--,t++)
  155.                 *((char *)t) = 0x42;
  156.         }
  157.         else
  158.         {
  159.             a-=tmp;
  160.             t+=tmp;
  161.             if(a >= 1)
  162.             {
  163.  
  164. #ifdef DEBUG
  165.                 ffp=Fseek(0L,fd,1);
  166.                 sprintf(tt,"[1][%d reset a>0 a=%ld|t=%lx tmp=%ld|fpos=%ld][Ok]",fd,a,t,tmp,ffp);
  167.                 puts(tt);
  168. #endif
  169.                 bufend=t;
  170. #ifdef DEBUG
  171.                 printf("bufend = %lx\n",bufend);
  172. #endif
  173.                 for( ; a>0 ; a--,t++)
  174.                     *((char *)t) = 0x42;
  175.             }
  176.         }
  177.     }
  178. }
  179.  
  180. /*
  181. void real_load(long l, char *b)
  182. {
  183.     long a=l;
  184.     long tmp,t=(long)b;
  185.  
  186. #ifdef DEBUG
  187.     long ffp;
  188.     char tt[128];
  189. #endif
  190.  
  191.     while (a > 0)
  192.     {
  193.  
  194. #ifdef DEBUG
  195.         ffp=Fseek(0L,fd,1);
  196.         sprintf(tt,"[1][%d: a=%ld|t=%lx fpos=%ld][Ok]",fd,a,t,ffp);
  197.         puts(tt);
  198. #endif
  199.             
  200.         tmp=Fread(fd, a & 0xfffffffeL, (void *)t);
  201.  
  202. #ifdef DEBUG
  203.         ffp=Fseek(0L,fd,1);
  204.         sprintf(tt,"[1][%d Read tmp=%ld|a=%ld|t=%lx fpos=%ld][Ok]",fd,tmp,a,t,ffp);
  205.         puts(tt);
  206. #endif
  207.  
  208.         if (tmp < 0)
  209.         {
  210. #ifdef DEBUG
  211.             ffp=Fseek(0L,fd,1);
  212.             sprintf(tt,"[1][%d reset tmp<0|a=%ld t=%lx|tmp=%ld fpos=%ld][Ok]",fd,a,t,tmp,ffp);
  213.             puts(tt);
  214. #endif
  215.             reset_file(fd);
  216.             a&=0xfffffffeL;
  217.  
  218.         }
  219.         else
  220.         {
  221.             a-=tmp;
  222.             t+=tmp;
  223.             if(a >= 1)
  224.             {
  225.  
  226. #ifdef DEBUG
  227.                 ffp=Fseek(0L,fd,1);
  228.                 sprintf(tt,"[1][%d reset a>0 a=%ld|t=%lx tmp=%ld|fpos=%ld][Ok]",fd,a,t,tmp,ffp);
  229.                 puts(tt);
  230. #endif
  231.                 reset_file(fd);
  232.                 if(tmp & 0x1L)
  233.                 {
  234.                     a--;        /* a&=0xfffffffeL; */
  235.                     t++;        /* t=(t&0xfffffffeL)+1; */
  236.                 }
  237.             }
  238.         }
  239.     }
  240. }
  241. */
  242.  
  243.  
  244. void load(int q)
  245. {
  246.     static int state = 0;
  247.     long ptr[4];
  248.     
  249. #ifdef DEBUG
  250.     long bo;
  251.     char tt[128];
  252. #endif
  253.  
  254.     buffptr(ptr);
  255.     buffer = ptr[0];
  256. #ifdef DEBUG
  257.     bo=buffoper(-1);
  258.     sprintf(tt,"buffer=%lx, buffoper=%lx",ptr[0],bo);
  259.     puts(tt);
  260. #endif
  261.     if (!q)
  262.     {
  263.         state = 0;
  264.         bufend=0;
  265.     }
  266.     else
  267.     {
  268.         if (has_stopped(state))
  269.         {
  270.             if(bufend)
  271.             {
  272. #ifdef DEBUG
  273.                 printf("Song reset!\n");
  274. #endif
  275.                 if(looping)
  276.                 {
  277.                     reset_file(fd);
  278.                     Supexec(reset_timerc);
  279.                     update_time();
  280.                     init_replay();
  281.                 }
  282.                 else
  283.                 {
  284.                     exit_replay();
  285.                     Supexec(reset_timerc);
  286.                     update_time();
  287.                 }
  288.             }
  289.             else
  290.             {
  291. #ifdef DEBUG
  292.                 printf("Song continues!\n");
  293. #endif
  294.                 savedtimerc=(200*Fseek(0L,fd,1))/(mp2info.bitrate/8);
  295.                 init_replay();
  296.                 Supexec(set_timerc);
  297.                 update_time();
  298.             }
  299.             return;
  300.         }
  301.     }
  302.  
  303.     if(!bufend)
  304.     {
  305.         if(state && (buffer < ((long)buffer_mem + block/2)) &&
  306.             (buffer > ((long)buffer_mem + STOP_BLOCK)))
  307.         {
  308.             real_load(block/2, (char *) ((long)buffer_mem + block/2));
  309.             set_stop((long *)buffer_mem);
  310.             state = !state;
  311.         }
  312.         else if(!q || (!state && (buffer >= ((long)buffer_mem + block/2 + STOP_BLOCK))))
  313.         {
  314.             real_load(block/2, buffer_mem);
  315.             set_stop((long *) ((long)buffer_mem + block/2));
  316.             state = !state;
  317.         }
  318.     }
  319. }
  320.  
  321. void set_stop(long *buf)
  322. {
  323.     int i;
  324.  
  325.     for (i=0 ; i<(STOP_BLOCK/4) ; i++)
  326.         buf[i] = 0x42424242L;
  327. }
  328.  
  329. int has_stopped(int state)
  330. {
  331.     if(bufend)
  332.     {
  333.         if(bufend < ((long)buffer_mem + block/2))
  334.         {
  335.             if((buffer > bufend) && (buffer < ((long)buffer_mem + block/2)))
  336.                 return 1;
  337.         }
  338.         else
  339.         {
  340.             if(buffer > bufend)
  341.                 return 1;
  342.         }
  343.     }
  344.     else
  345.     {
  346.         if(state && (buffer > ((long)buffer_mem + block/2)) &&
  347.             (buffer < ((long)buffer_mem + block/2 + STOP_BLOCK)))
  348.             return 1;
  349.         else if(!state && (buffer > (long)buffer_mem) &&
  350.             (buffer < ((long)buffer_mem + STOP_BLOCK)))
  351.             return 1;
  352.     }
  353.  
  354.     return 0;
  355. }
  356.